
package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * 江门网
 */
public class TaijiaobbsAnalyse  implements AnalyseNews{

	
	public boolean isDetailPage(String url){
		boolean bRet = false;
		String[] regex = {
				"http://www.taijiaobb.cn/bbs/thread-[0-9]*-1-1.html"
				//http://www.taijiaobb.cn/bbs/thread-152080-1-1.html
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;
		String author = null;

		Document doc = Jsoup.parse(htmltxt);
		title =doc.select("b.blue").text();
		if(doc.select("b.blue").size() > 2)
			title =doc.select("b.blue").get(2).text();

		content = HtmlCleaner.getContentHtml(url, doc.select(".t_msgfont").first());


		date = DateUtils.matchDate(doc.select(".t_msg span").text());
		if( doc.select(".t_user b").size() > 0)
			author = doc.select(".t_user b").get(0).text();

		try {
			String clickStr = doc.select("b.orange").text();
			String commentStr = doc.select("b.green").text();
			clickNum =NumberUtils.matchNumber(clickStr);
			commentNum = NumberUtils.matchNumber(commentStr);
		} catch (Exception e) {
			clickNum = 0;
			commentNum = 0;
		}
		if(StringUtils.isBlank(title)){
		}
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setAuthor(author);
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setUpdateUrl(url);
		return bbs;
	}
	
//	public NewsMeta Update(NewsMeta meta) {
//		if(meta!=null){
//			String updateUrl = meta.getUpdateUrl();
//			if(updateUrl!=null){
//				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
//				if(responseToURL!=null){
//					String htmltxt = responseToURL.getHtml();
//					Document doc = Jsoup.parse(htmltxt);
//					Integer clickNum=0;
//					Integer commentNum=0;
//					try {
//						if(doc.select("td.pls div.hm span.xi1").size()==2){
//							String clickStr = doc.select("td.pls div.hm span.xi1").get(0).text();
//							String commentStr = doc.select("td.pls div.hm span.xi1").get(1).text();
//							clickNum = Integer.parseInt(clickStr);
//							commentNum = Integer.parseInt(commentStr);
//						}
//					} catch (Exception e) {
//						clickNum = 0;
//						commentNum = 0;
//					}
//					meta.setClickNum(clickNum);
//					meta.setCommentNum(commentNum);
//					return meta;
//				}
//			}
//		}
//		return null;
//	}
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

    
	
}
